//
//  SLCAEmitterLayerController.m
//  Objective-C
//
//  Created by 陆承东 on 2018/7/1.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLCAEmitterLayerController.h"

@interface SLCAEmitterLayerController ()

@property (nonatomic, weak) UIView *containerView;

@end

@implementation SLCAEmitterLayerController

- (void)initNavBar {
    self.navTitle = @"CAEmitterLayer";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavBar];
    
    [self initView];
}

- (void)initView {
    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    containerView.backgroundColor = [UIColor whiteColor];
    self.containerView = containerView;
    [self.view addSubview:containerView];
    
    // create particle emitter layer
    CAEmitterLayer *emitterLayer = [CAEmitterLayer layer];
    emitterLayer.frame = self.containerView.bounds;
    [self.containerView.layer addSublayer:emitterLayer];
    
    // configure emitter
    emitterLayer.renderMode = kCAEmitterLayerAdditive;
    emitterLayer.emitterPosition = CGPointMake(emitterLayer.frame.size.width / 2.0, emitterLayer.frame.size.height / 2.0);
    
    // create a particle template
    CAEmitterCell *emitterCell = [[CAEmitterCell alloc] init];
    emitterCell.contents = (__bridge id)[UIImage imageNamed:@"nav_share"].CGImage;
    emitterCell.birthRate = 30;
    emitterCell.lifetime = 5.0;
    emitterCell.color = [UIColor colorWithRed:1.0 green:0.5 blue:1.0 alpha:1.0].CGColor;
    emitterCell.alphaSpeed = -0.4;
    emitterCell.velocity = 50;
    emitterCell.velocityRange = 50;
    emitterCell.emissionRange = M_PI_4;
    
    // add particle template to emitter
    emitterLayer.emitterCells = @[emitterCell];
}

@end
